package com.tj.jinzao.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.tj.jinzao.R;
import com.tj.jinzao.api.ApiRetrofit;
import com.tj.jinzao.api.ApiService;
import com.tj.jinzao.api.SubscriberCallBack;
import com.tj.jinzao.base.BaseActivity;
import com.tj.jinzao.manager.UserManager;
import com.tj.jinzao.model.MessageAuthBean;
import com.tj.jinzao.model.ResultResponse;
import com.tj.jinzao.model.User;
import com.tj.jinzao.utils.ToastUtils;
import com.tj.jinzao.widgets.MaxTextLengthFilter;
import com.tj.jinzao.widgets.MaxTextLengthFilterIH;

import java.util.HashMap;

import butterknife.OnClick;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * 我的身份认证界面
 * Created by wjy on 2019/1/5.
 */
public class MyIdentificationOccupation2Activity extends BaseActivity {
    protected ApiService mApiService = ApiRetrofit.getInstance().getApiService();
    private Button bt_submit;
    private TextView tv_occupation,tv_number;

    private CompositeSubscription mCompositeSubscription;
    private User user;
    EditText et_desc;
    @Override
    protected void setupView() {
        super.setupView();
        bt_submit=findViewById(R.id.bt_submit);
        tv_occupation=findViewById(R.id.tv_occupation);
        tv_number=findViewById(R.id.tv_number);
        user=UserManager.getInstance().getUser();
        et_desc=findViewById(R.id.et_desc);

        et_desc.setFilters(new InputFilter[]{new MaxTextLengthFilterIH(300,MyIdentificationOccupation2Activity.this,"内容限300字以内")});
        et_desc.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(!TextUtils.isEmpty(et_desc.getText())){
                    tv_number.setText(et_desc.getText().toString().length()+"/300");
                }else {
                    tv_number.setText("0/300");
                }


            }
        });

        if(user.getAuthentication()==2){
            bt_submit.setText("认证通过");
            bt_submit.setBackgroundResource(R.drawable.bg_login_grey);
        }else if(user.getAuthentication()==4){
            bt_submit.setText("审核中");
            bt_submit.setBackgroundResource(R.drawable.bg_login_grey);
        }else {
            bt_submit.setText("提交");
            bt_submit.setBackgroundResource(R.drawable.bg_login_button);
        }
    }

    @Override
    public int getLayoutResource() {
        return R.layout.activity_my_identification_occupation_2;
    }

    @Override
    protected int getLeftButtonResId() {
        return R.mipmap.ic_back;
    }

    @OnClick(R.id.bt_submit)
    public void submit() {
        if(user.getAuthentication()==2||user.getAuthentication()==4){
            return;
        }
        if(TextUtils.isEmpty(et_desc.getText())){
            ToastUtils.showToast(MyIdentificationOccupation2Activity.this,"请输入描述说明");
            return;
        }
        HashMap<String,Object> hashMap=new HashMap<>();
        hashMap.put("uid", UserManager.getInstance().getUser().getId());
        hashMap.put("job",title);
        hashMap.put("jobItem",name);
        hashMap.put("imgurls",getIntent().getStringExtra("image"));
        hashMap.put("description",et_desc.getText().toString());
        addSubscription(mApiService.getTrue(hashMap), new SubscriberCallBack<MessageAuthBean>(this) {
            @Override
            protected void onSuccess(MessageAuthBean list) {
                user.setAuthentication(4);
                UserManager.saveUserData(user);
                ToastUtils.showToast(MyIdentificationOccupation2Activity.this,"身份认证已提交");
                finish();
            }

            @Override
            protected void onFailure(ResultResponse response) {
                ToastUtils.showToast(MyIdentificationOccupation2Activity.this,response.getMessage());
            }

            @Override
            protected boolean isShowProgress() {
                return false;
            }

            @Override
            protected void onPageSuccess(int currentPage, int allPage, int allSize) {
            }
        });

    }
    private String title="医生",name="骨科";

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK){
            if(requestCode==101){
                title=data.getStringExtra("title");
                name=data.getStringExtra("name");
                tv_occupation.setText(name);
            }
        }
    }

    @OnClick(R.id.rl_occupation_layout)
    public void chooseOccupation() {
        Intent to=new Intent();
        to.setClass(this, ChooseOccupationActivity.class);
        startActivityForResult(to,101);
    }
    public void addSubscription(Observable observable, Subscriber subscriber) {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }
        mCompositeSubscription.add(observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber));
    }

    //RXjava取消注册，以避免内存泄露
    public void onUnsubscribe() {
        if (mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions()) {
            mCompositeSubscription.unsubscribe();
        }
    }

}
